/*****************************************************************************
 *   Copyright 2005 Tim A Wang
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 *
 ****************************************************************************/
package com.jb.web.renderer;

import com.jb.web.bean.Panel;
import com.jb.web.bean.Label;
import com.jb.web.bean.TabbedPanel;
import com.jb.web.context.WebContext;

import java.util.Iterator;

import com.jb.faces.component.UIComponent; 

public class AJaxTabbedTabRenderer extends RendererImpl {
    public StringBuffer renderBegin(WebContext pCtx, UIComponent itemToRender) {
        StringBuffer html = new StringBuffer();
        TabbedPanel sect = (TabbedPanel) itemToRender;
        html.append("<div ").append(getEditScript(pCtx, sect))
            .append(addTagProperty("class", sect.getStyleClass(), null))
            .append(addTagProperty("style", sect.getStyle(pCtx), null))
            .append(">");

        return html;
    }

    public StringBuffer renderChildren(WebContext pCtx, UIComponent itemToRender) {
        StringBuffer html = new StringBuffer();
        TabbedPanel sect = (TabbedPanel) itemToRender;
        Panel subSect = sect.getSelected();

        if (subSect != null) {
            html.append(subSect.render(pCtx));
        }
        if(!sect.isHideTabs() || !pCtx.isRunTime())
            html.append(renderTabs(pCtx, sect));
        return html;
    }

    public StringBuffer renderEnd(WebContext pCtx, UIComponent itemToRender) {
        return new StringBuffer("</div>");
    }

    //End of your change
    protected String renderTabs(WebContext pCtx, TabbedPanel sect) {
        Label header = sect.getHeader();
        StringBuffer html = new StringBuffer("<table cellpadding=\"0\" cellSpacing=\"0\"  border=\"0\" ")
            .append(addTagProperty("width", "100%", null))
            .append(addTagProperty("id", sect.getClientId()+"table", null))
            .append("><TR><td style=\"width:12px;background-color:transparent;\" >&nbsp;</td>");

        String headLabel = evaluate(pCtx, header, header.getValue());

        if (isEmptyString(headLabel)) {
            headLabel = "&nbsp;&nbsp";
        }

        if ("right".equalsIgnoreCase(sect.getHeaderAlign())) {
            html.append("<td align=\"left\" style=\"width:100%;background-color:transparent;\" ><SPAN ")
                .append(getEditScript(pCtx, header))
                .append(addTagProperty("style", header.getStyle(pCtx), null))
                .append(" >");

            if (!isEmptyString(header.getIconImage())) {
                html.append("<img class=\"jbLabelImg\" ").append(addTagProperty("src", header.getIconImage(), null))
                    .append(" />");
            }

            html.append(headLabel);
            html.append("</span></td>");
        }

        Iterator iter = sect.getChildPanels().iterator();

        while (iter.hasNext()) {
            Panel aSect = (Panel) iter.next();

            if (aSect != null) {
                Label aTitle = aSect.getHeader();

                if (!aTitle.isVisible(pCtx)) {
                    continue;
                }

                String label =  evaluate(pCtx, aTitle, aTitle.getValue());

                if (isEmptyString(label)) {
                    label = aSect.getId();
                }

                if (sect.isSelected(aSect)) {
                    String headStyle = aTitle.getStyle(pCtx);

                    html.append("<td valign=\"bottom\"><div ");
                    html.append(getEditScript(pCtx, aTitle))
                        .append(addTagProperty("class", aTitle.getStyleClass(), null))
                        .append(addTagProperty("style", headStyle, null)).append(">");

                    if (!isEmptyString(aTitle.getIconImage())) {
                        html.append("<img  class=\"jbLabelImg\" ")
                            .append(addTagProperty("src", aTitle.getIconImage(), null)).append(" />");
                    }

                    html.append(label);
                    html.append("</div></td>");
                } else {
                    String headStyle = aTitle.getStyle(pCtx);
                    html.append("<td valign=\"bottom\"><div ");

                    html.append(addTagProperty("class", header.getStyleClass(), null))
                        .append(addTagProperty("style", headStyle, null))
                      //  .append(getEditScript(pCtx, header))
                        .append(" onclick=").append("\"JB._e('").append(pCtx.getPageName() + ".xsp").append("','")
                        .append(aSect.getClientId()).append("','").append("com.jb.web.bean.event.OnSelectTab")
                        .append("','").append(sect.getClientId()).append("');\"").append(">");

                    if (!isEmptyString(aTitle.getIconImage())) {
                        html.append("<img  class=\"jbLabelImg\" ")
                            .append(addTagProperty("src", aTitle.getIconImage(), null)).append(" />");
                    }

                    html.append(label);
                    html.append("</div></td>");
                }
            }
        }

        if (!("right".equalsIgnoreCase(sect.getHeaderAlign()))) {
            html.append("<td align=\"right\" style=\"width:100%;background-color:transparent;\" ><SPAN ")
                .append(getEditScript(pCtx, header))
                            .append(addTagProperty("style", header.getStyle(pCtx), null)).append(" >");

            if (!isEmptyString(header.getIconImage())) {
                html.append("<img class=\"jbLabelImg\" ").append(addTagProperty("src", header.getIconImage(), null))
                    .append(" />");
            }

            html.append(headLabel).append("</span></td>");
        }

        html.append("<td style=\"width:12px;background-color:transparent;\" >&nbsp;</td></TR></table>");

        return html.toString();
    }
}
